package spring.cloud.tasks.api.analyzer;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.NameSpaceConfiguration;
import spring.cloud.tasks.common.node.BlockNodePaths;
import spring.cloud.tasks.common.node.TaskNodePath;
import spring.cloud.tasks.common.task.TaskIdAndTaskInstanceIdAndTaskTurnIdAndTaskItemIndex;
import spring.cloud.tasks.common.task.TaskItemExecuteRenewalInfo;
import spring.cloud.tasks.common.zookeeper.ZooKeeper;

import java.util.List;

@Slf4j
@Setter
@Getter
public class TimeoutTaskAnalyzer {


    /**
     * 查找超时告警任务
     */
    public void analyzeTimeoutTask(NameSpaceConfiguration nameSpaceConfiguration, ZooKeeper zooKeeper, List<TimeoutTask> timeoutTaskList, String taskId) {
        TimeoutTask timeoutTask = new TimeoutTask(nameSpaceConfiguration.getNameSpace(), taskId);
        if (isTimeout(zooKeeper, timeoutTask, timeoutTaskList)) {
            timeoutTaskList.add(timeoutTask);
        }
    }


    /**
     * 如果配置了超时告警时间，而且running节点存在时间大于它，则告警
     */
    private boolean isTimeout(ZooKeeper zooKeeper, TimeoutTask timeoutTask, List<TimeoutTask> oldTimeoutTaskList) {
        String taskId = timeoutTask.getTaskId();
        int timeout4AlarmSeconds = getTimeout4AlarmSeconds(zooKeeper, taskId);
        if (timeout4AlarmSeconds <= 0) {
            return false;
        }
        zooKeeper.getChildren(TaskNodePath.getTaskItemsNodePath(taskId));
        List<TaskIdAndTaskInstanceIdAndTaskTurnIdAndTaskItemIndex> taskInstanceIdAndTaskTurnIdAndTaskItemIndexList = BlockNodePaths.getTaskIdAndTaskInstanceIdAndTaskTurnIdAndTaskItemIndexList(zooKeeper, taskId);
        if (taskInstanceIdAndTaskTurnIdAndTaskItemIndexList == null || taskInstanceIdAndTaskTurnIdAndTaskItemIndexList.isEmpty()) {
            return false;
        }
        computeTimeoutItems(zooKeeper, taskId, taskInstanceIdAndTaskTurnIdAndTaskItemIndexList, timeout4AlarmSeconds, timeoutTask);
        if (timeoutTask.getTaskInstanceAndTaskItemList().isEmpty()) {
            return false;
        } else {
            return true;
        }

    }

    private int getTimeout4AlarmSeconds(ZooKeeper zooKeeper, String taskId) {
        String timeout4AlarmSecondsStr = zooKeeper.getData(TaskNodePath.getTaskConfigurationNodePath(taskId, "timeout4AlarmSeconds"));
        int timeout4AlarmSeconds = 0;
        if (timeout4AlarmSecondsStr != null) {
            try {
                timeout4AlarmSeconds = Integer.parseInt(timeout4AlarmSecondsStr);
            } catch (NumberFormatException e) {
                log.error(e.getMessage(), e);
            }
        }
        return timeout4AlarmSeconds;
    }

    private void computeTimeoutItems(ZooKeeper zooKeeper, String taskId, List<TaskIdAndTaskInstanceIdAndTaskTurnIdAndTaskItemIndex> taskIdAndTaskInstanceAndTaskItemList, int timeout4AlarmSeconds, TimeoutTask timeoutTask) {
        timeoutTask.setTimeout4AlarmSeconds(timeout4AlarmSeconds);
        for (TaskIdAndTaskInstanceIdAndTaskTurnIdAndTaskItemIndex taskIdAndTaskInstanceAndTaskItem : taskIdAndTaskInstanceAndTaskItemList) {
            //分配但是没有执行器进行续期执行
            TaskItemExecuteRenewalInfo taskItemExecuteRenewalInfo = BlockNodePaths.getTaskItemExecuteRenewalInfo(zooKeeper, taskIdAndTaskInstanceAndTaskItem);
            if (taskItemExecuteRenewalInfo != null) {
                if (taskItemExecuteRenewalInfo.getExecutorTaskThreadRunReportTime() > taskItemExecuteRenewalInfo.getExecutorMaxAllowExecuteAlarmTime()) {
                    timeoutTask.getTaskInstanceAndTaskItemList().add(taskIdAndTaskInstanceAndTaskItem);
                }
            }
        }
    }


}
